by Gene Olafsen
In This Chapter
One of the most visible incarnations of COM is ActiveX controls. Visual C++ 6.0 generally refers to ActiveX controls as simply controls. The control component group encompasses a wide range of component offerings from familiar image buttons to network communication libraries.
Controls built with COM are, to a certain degree, descendents of an earlier initiative by Microsoft to plug a third-party interface into a language. The language was Visual Basic and the plug-in standard was the VBX. It didnt take long for developers to embrace VBXs, and soon there were literally hundreds on the market, which enabled developers to implement everything from modem communication protocols to animated picture buttons. The problem with VBXs was that as Visual Basic grew in sophistication, the VBX specification left little room for the scope Microsoft had in mind. Also, VBXs were firmly grounded in the Visual Basic language. It would be great if a programming-language-neutral third-party interface could be defined. Here comes COM to the rescue.
A plug-in or control based on the original COM control specification required implementing a fairly hefty number of interfaces. These interfaces included those that implement property sheets, display a user interface, and manage container-control communication. For most controls, this specification was overkill. The tools and documentation that were available to aid in the development in such compliant controls were not up to the job. The resulting controls occupied a sizeable memory footprint, and activation was lethargic. To combat these problems, Microsoft introduced incremental improvements, including a number of control-container extensions that sped up activation and reduced UI requirements.
As the browser wars started to heat up, and Java and the JavaBean specification (a Java-based component architecture) started to take shape, Microsofts browser became a control container. A sticking issue was the fact that the controls were still pretty big, and took a relatively long time to download to the browserremember that most modems were cranking along at 2400 to 9600 baud. The control specification was changed one last time, where it remains today.
The control requirements are as follows:
The specification of a control is thus so broad that it encompasses just about any COM server. This is exactly the pointin this manner, Microsoft defines a requirement set that has minimal overhead and allows the developer to produce components that are tailored for an exact purpose.
Both MFC and ATL support control development. In fact, both make it relatively painless to develop controls through the support of various wizards and powerful helper classes or templates. There are certain advantages to creating your control using either framework, and it is important to understand the major issues with each before beginning development.
The Microsoft Foundation Classes have been around for a long time, and you are certainly familiar with the object hierarchy, even if you havent explored control development. Because there can be a lot more to control development than simply generating the skeleton code and pointing to the controls device context, your familiarity with the MFC collection classes, runtime identification, and creation and serialization mechanisms can be a big advantage in writing the code for the control.
There are, however, some disadvantages to creating a control with MFC. One of the easiest ways to determine whether it is appropriate for you to build a control using MFC is to identify the context in which you will use the control. If you are building the control as part of the process of componentizing an installable application, or you intend the control for use as part of a toolkit for another developers application, basing the control on MFC is appropriate. If, however, you expect control deployment to occur on an HTML page, you might have to consider several points.
Controls built using MFC require the presence of the appropriate MFC DLLs for the control to execute. This might not be an issue for controls in use as part of an installable application, because the libraries can be included on the distribution media, but it can be an issue with use in a browser. The end user can navigate to a page on the Internet with an ActiveX control that is built with MFC, and the appropriate libraries might not be present on the users system. In addition, controls built using MFC can become quite large, and downloading the control can be frustrating to the end user. The DLLs weigh in at over one megabyte in size, so download is tedious at best. This is not to say that you cannot build a lightweight control using MFC, but if size is an issue, there is an alternative: the Active Template Library (ATL).
The Active Template Library offers you a lightweight alternative to controls built using MFC. The term lightweight doesnt necessarily refer to the fact that controls built with ATL are less powerful than their MFC counterparts; rather, it refers to the fact that this new framework carries around less baggage than its older sibling.
ATL utilizes templates (no surprise there) in the creation of COM components. A template, at the most basic level of understanding, offers you a way to specify a generic representation of a class and a template and then create parameterized instances of the class. The ATL framework offers a large number of templates for many of the standard COM interfaces. In use, templates differ from a class hierarchy in that there is no additional complexity as the result of inheritance. The resulting class doesnt depend on overriding virtual functions, thus there is no overhead in terms of execution penalty or object size because the functionality is not dependent on vtables. In summary, the footprint for ATL-based components is smaller than for the equivalent MFC-based control.